package com.zjc.springboot.nettyclient.client;

import com.zjc.springboot.nettyclient.channelInitializer.NettyChannelInitializer;
import com.zjc.springboot.nettyclient.config.BaseConfig;
import com.zjc.springboot.nettyclient.config.Config12;
import com.zjc.springboot.nettyclient.v12.Protocol12;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import java.util.HashMap;

public class NettyClient12 implements NettyClient {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private HashMap<String, String> params = new HashMap<>();

    private Config12 config12;

    public NettyClient12(Config12 config12) {
        this.config12 = config12;
    }

    private SocketChannel socketChannel = null;

    private boolean isConnect = false;

    @PostConstruct
    public void start() throws InterruptedException {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        bootstrap.group(eventLoopGroup);
        bootstrap.remoteAddress(this.getConfig().getHost(), this.getConfig().getPort());
        bootstrap.handler(new NettyChannelInitializer(this));
        ChannelFuture future = bootstrap.connect(this.getConfig().getHost(), this.getConfig().getPort()).sync();
        if (future.isSuccess()) {
            socketChannel = (SocketChannel) future.channel();
            isConnect = true;
            log.info("connect server success");
            this.login();
        }
    }

    public SocketChannel getSocketChannel() {
        return socketChannel;
    }

    public void login() {
        this.sendMessage(Protocol12.getLoginStr(this.getConfig().getTerminalId()));

    }

    public void sendPositionMessage() {
        this.sendMessage(Protocol12.getLatLonStr(this.getConfig().getTerminalId(), this.getConfig().getLat(), this.getConfig().getLon()));

    }


    public void sendMessage(Object msg) {
        if (socketChannel != null) {
            if (msg instanceof String) {
                socketChannel.writeAndFlush(msg);
            }
        }
    }

    public boolean isConnect() {
        return isConnect;
    }

    public void setConnect(boolean connect) {
        isConnect = connect;
    }

    public BaseConfig getConfig() {
        return config12;
    }

    @Override
    public void setParam(String key, String value) {
        this.params.put(key, value);
    }

    @Override
    public String getParam(String key) {
        return this.params.get(key);
    }

    public void setConfig(Config12 config12) {
        this.config12 = config12;
    }

}

